package controller;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;

import model.Layer;
import utils.ImageUtils;
import view.MainFrame;
import abstraction.ImageOperationDialog;
import dialog.OpacityLayerDialog;

public class LayerController
{

    /**
     * resize a layer
     * @param resizeDimension
     */
    public static void resizeLayerTo(Dimension resizeDimension) {
    	Layer layerSelected = MainFrame.getInstance().getDrawingPane()
    			.getSelectedLayer();
    	layerSelected.resizeImage((int) resizeDimension.getWidth(),
    			(int) resizeDimension.getHeight());
    	layerSelected.setSize(resizeDimension);
    }

    /**
     * Change the opacity of a layer
     */
    public static void changeOpacity(Layer layer) {
    	
    	int defaultValue = layer.getAlpha();
    	ImageOperationDialog dialog = new OpacityLayerDialog(
    			MainFrame.getInstance(), layer);
    	dialog.setVisible(true);
    
    	if (dialog.getReturnStatus() == ImageOperationDialog.RET_OK) {
    		layer.setImage(ImageUtils.deepCopy(dialog
    				.getTransformedImage()));
    		MainFrame.getInstance().repaint();
    	} else {
    		layer.setAlpha(defaultValue);
    	}
    }

    /**
     * Draw a line on a layer
     * @param selectedLayer
     * @param color
     * @param prevX : from this
     * @param prevY : from this
     * @param x : to this
     * @param y : to this
     */
    public static void drawPoint(Layer selectedLayer, Color color, int prevX, int prevY, int x, int y)
    {
        if (selectedLayer.getX() < x && selectedLayer.getY() < y){
            selectedLayer.drawPoint(color, 
                    prevX - selectedLayer.getX(),
                    prevY - selectedLayer.getY(),
                    x - selectedLayer.getX(), 
                    y - selectedLayer.getY());
            MainFrame.getInstance().getDrawingPane().repaint();
        }
    }

    /**
     * see drawPoint
     * @param selectedLayer
     * @param color
     * @param size
     * @param prevX
     * @param prevY
     * @param x
     * @param y
     */
    public static void drawBrush(Layer selectedLayer, Color color, int size, int prevX, int prevY, int x, int y)
    {
        if (selectedLayer.getX() < x && selectedLayer.getY() < y){
            selectedLayer.drawBrush(color, size,
                    prevX - selectedLayer.getX(),
                    prevY - selectedLayer.getY(),
                    x - selectedLayer.getX(), 
                    y - selectedLayer.getY());
            MainFrame.getInstance().getDrawingPane().repaint();
        }
    }

    public static void erase(Layer selectedLayer, int size, int x, int y)
    {
        if (selectedLayer.getX() < x && selectedLayer.getY() < y){
            selectedLayer.erase(size,
                    x - selectedLayer.getX(), 
                    y - selectedLayer.getY());
            MainFrame.getInstance().getDrawingPane().repaint();
        }
    }

    /**
     * see drawPoint
     * @param selectedLayer
     * @param color
     * @param size
     * @param x
     * @param y
     */
    public static void drawSpray(Layer selectedLayer, Color color, int size, int x, int y)
    {
        if (selectedLayer.getX() < x && selectedLayer.getY() < y){
            selectedLayer.drawSpray(color, size,
                    x - selectedLayer.getX(), 
                    y - selectedLayer.getY());
            MainFrame.getInstance().getDrawingPane().repaint();
        }
    }

    /**
     * rotate the layer by an angle
     * @param layer
     * @param angle
     */
    public static void rotateLayer(Layer layer,int angle){
    	int index = MainFrame.getInstance().getDrawingPane()
    			.getSelectedLayerIndex();		
    	int  hypot = (int) (1+Math.sqrt(Math.pow(layer.getWidth(), 2) + Math.pow(layer.getHeight(), 2)));
    	Layer backgroundLayerToMix = new Layer(hypot, hypot, Color.YELLOW, layer.getTitle(), 0);
    	backgroundLayerToMix.setLocation(layer.getX()-(hypot-layer.getWidth())/2, layer.getY()-(hypot-layer.getHeight())/2);
    	MainFrame.getInstance().getDrawingPane().addLayer(backgroundLayerToMix);
    	LayeredPaneController.fusionLayers(index + 1, 0);
    	backgroundLayerToMix.rotate(angle);
    }

    /**
     * cut from topleft to bottomright and put it in a new layer which is add to the model
     * @param topLeftPoint
     * @param bottomRightPoint
     */
    public static void cutImageTo(Point topLeftPoint, Point bottomRightPoint)
    {
        Layer layerSelected = MainFrame.getInstance().getDrawingPane().getSelectedLayer();
        
        Layer newLayer = new Layer(layerSelected, topLeftPoint, bottomRightPoint);
        
        MainFrame.getInstance().getDrawingPane().addLayer(newLayer);
        
        newLayer.setLocation((int)topLeftPoint.getX()+layerSelected.getX(),(int)topLeftPoint.getY()+layerSelected.getY());
    }

}
